package com.surfilter.massdata.spark.input;

import java.util.HashMap;
import java.util.Map;

import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.api.java.function.Function;
import org.apache.spark.sql.DataFrame;
import org.apache.spark.sql.SQLContext;

import com.act.sparkanalyz.input.ISparkInput;
import com.act.sparkanalyz.service.impl.SparkService.ServiceContext;
import com.surfilter.massdata.spark.bean.Domain_history;
/*
 * 
 */
public class FileSpark implements ISparkInput{
 private String path;
 private String tableName;
 private String splitChar;
	@Override
	public Map<String, DataFrame> read(ServiceContext serviceContext, Map<String, String> commandMap) throws Exception {
	
			SQLContext sqlContext = serviceContext.getSqlCtx();
			JavaRDD<String> lines = serviceContext.getCtx().textFile(path = this.getPath(path));
			JavaRDD<Domain_history> rowRDD=lines.map(new Function<String,Domain_history>(){

				@Override
				public Domain_history call(String line) throws Exception {
					Domain_history domain=new Domain_history();
					String arr[]=line.split(splitChar);
					domain.setDomain(arr[0]);
					domain.setIP(arr[1]);
					domain.setDateTime(arr[2]);
	                domain.setActive_static(Integer.valueOf(arr[3]));
	                domain.setLive_static(Integer.valueOf(arr[4]));
					return domain;
				}	
			});	
		org.apache.spark.sql.DataFrame dataFrame=sqlContext.createDataFrame(rowRDD, Domain_history.class);
		
		dataFrame.registerTempTable(tableName);
		//dataFrame.show();
		Map<String, DataFrame> map=new HashMap<String,DataFrame>();
		map.put(tableName, dataFrame);
		return map ;
	}
	private String getPath(String path) {
		
		return path;
	}

}
